import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.windfly.util.DateUtil;
import cn.windfly.util.JpaEntityBase;
import cn.windfly.util.StringUtil;

public class ProducerConsumer {
	public static void main(String[] args) {
		Object v = "19:02:12";
		if (v .toString().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")) {
			v = DateUtil.prase(v.toString().trim());
		} else if (v.toString().matches("\\d{4}-\\d{2}-\\d{2}")) {
			v = DateUtil.prase(v.toString().trim(), "yyyy-MM-dd");
		} else if (v.toString().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}")) {
			v = DateUtil.prase(v.toString().trim(), "yyyy-MM-dd HH:mm");
		} else if (v.toString().matches("\\d{4}-\\d{2}")) {
			v = DateUtil.prase(v.toString().trim(), "yyyy-MM");
		} else if (v.toString().matches("\\d{4}")) {
			v = DateUtil.prase(v.toString().trim(), "yyyy");
		} else if (v.toString().matches("\\d{2}:\\d{2}:\\d{2}")) {
			v = DateUtil.prase(v.toString().trim(), "HH:mm:ss");
		} else {
			v = new Date(v.toString());
		}
		
		System.out.println(((Date)v).toLocaleString());
	}

	private static void b() {
		ArrayList<String> idStrings = new ArrayList<>();
		idStrings.add("040022331222");
		idStrings.add("040022331221");
		String p = "'"+StringUtil.join(idStrings,"','")+"'";
		System.out.println(p);
		String s = " select b.gps_id,b.gps_datetime,b.gps_mile,c.carno,t.name FROM  (SELECT * FROM ((select * from track180913 )  union (select * from track181018 )  union (select * from track181019 )  union (select * from track181020 )  union (select * from track181021 )  union (select * from track181120 ) ) u  ORDER BY gps_datetime ASC) b left join car c on c.gps_id = b.gps_id left join transportcompany t on t.id =c.transportcompanyid where b.gps_id in ("+p+") group by  b.gps_id ";
		List<Map<String, Object>> simpleSearchSql2 = JpaEntityBase.simpleSearchSql2(s);
		System.out.println(simpleSearchSql2);
	}

	private static void a() {
		SyncStack ss = new SyncStack();
		Producer p = new Producer(ss);
		Consumer c = new Consumer(ss);
		new Thread(p).start();
		new Thread(c).start();
	}
}

class Wotou {
	int id;

	Wotou(int id) {
		this.id = id;
	}

	public String toString() {
		return "Wotou" + id;
	}
}

class SyncStack {
	int index = 0;
	Wotou[] arrWotou = new Wotou[6];

	public synchronized void push(Wotou wt) {
		if (index == arrWotou.length) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify();
		arrWotou[index] = wt;
		index++;
	}

	public synchronized Wotou pop() {
		if (index == 0) {
			try {
				this.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.notify();
		index--;
		return arrWotou[index];
	}
}

class Producer implements Runnable {
	SyncStack ss = null;

	Producer(SyncStack ss) {
		this.ss = ss;
	}

	public void run() {
		for (int i = 0; i < 100; i++) {
			Wotou wt = new Wotou(i);
			ss.push(wt);
			// System.out.println("produce Wotou" + i);
			
			try {
				int a = (int) (Math.random() * 1000);
				System.out.println("produce  " + wt +" "+a);
				Thread.sleep(a);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

		}
	}
}

class Consumer implements Runnable {
	SyncStack ss = null;

	Consumer(SyncStack ss) {
		this.ss = ss;
	}

	public void run() {
		for (int i = 0; i < 100; i++) {
			Wotou wt = ss.pop();

			try {
				int a = (int) (Math.random() * 1000);
				System.out.println("consum     " + wt +" "+a);
				Thread.sleep(a);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
